home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 1.iso / games / worm.zip / WORMDRAW.C < prev    next >
C/C++ Source or Header  |  1992-09-21  |  4KB  |  145 lines

  1.  
  2.  
  3. #include "worm.h"
  4.  
  5. RING *rP;
  6. static RECT  _wormcage;
  7.  
  8. /* ----------------------------------------------------------------- */
  9.  
  10. BOOL GetScreenSize(void)
  11. {
  12.    HDC    screen;
  13.  
  14.    /* Create a device context for the screen. */
  15.    screen=CreateDC("DISPLAY", NULL, NULL, NULL);
  16.    if(!screen)
  17.        return(FALSE);
  18.  
  19.    _wormcage.right  = GetDeviceCaps(screen, HORZRES);
  20.    _wormcage.bottom = GetDeviceCaps(screen, VERTRES);
  21.  
  22.    /* Delete the device context as soon as possible. */
  23.    DeleteDC(screen);
  24.  
  25.    _wormcage.top  = 0;
  26.    _wormcage.left = 0;
  27.  
  28.    /* This creates a worm list. */
  29.    rP = InitWormList();
  30.    return(TRUE);
  31. }
  32.  
  33. /* ----------------------------------------------------------------- */
  34.  
  35. void WormUpdate(void)
  36. {
  37. HDC screen;
  38. static double dir=0.0;
  39. POINT  currentTail, currentHead, previousHead;
  40. int size     = GetSize();
  41. int stepSize = GetStep() + size;
  42.  
  43.  
  44. /* rP points to the head */
  45. previousHead  = rP->center;
  46. currentTail   = rP->next->center;
  47.  
  48. /* make rP point to the tail: */
  49. rP = rP->next;
  50.  
  51. #if 1 /* if using a 386 capable compiler */
  52. if(LSFR())
  53.     dir+=INCREMENT;
  54. else
  55.     dir-=INCREMENT;
  56. #endif
  57. #if 0 /* otherwise use the standard rand() */
  58. if(rand()<16384)
  59.    dir+=INCREMENT;
  60. else
  61.    dir-=INCREMENT;
  62. #endif
  63.  
  64. /* update the coordinates of the new head (i.e the previous tail) */
  65. CALCULATE_DIR:
  66.    rP->center.x  = previousHead.x + (int)(stepSize*cos(dir));
  67.    rP->center.y  = previousHead.y - (int)(stepSize*sin(dir));
  68.  
  69.  
  70.    if(rP->center.x < _wormcage.left)
  71.       rP->center.x = _wormcage.right-1;
  72.    if(rP->center.x > _wormcage.right)
  73.       rP->center.x = _wormcage.left;
  74.    if(rP->center.y < _wormcage.top)
  75.       rP->center.y = _wormcage.bottom-1;
  76.    if(rP->center.y > _wormcage.bottom)
  77.       rP->center.y = _wormcage.top;
  78.  
  79.  
  80.    screen=CreateDC("DISPLAY", NULL, NULL, NULL);
  81.    SelectObject(screen, GetStockObject(BLACK_BRUSH) );
  82.    SetROP2(screen, R2_NOTXORPEN);
  83.    SelectObject(screen, GetStockObject(BLACK_PEN) );
  84.  
  85.    /* erase the currentTail */
  86.    if( (IsOnDesktop(currentTail)) && (rP->state == ACTIVE) &&
  87.        (rP->color == GetPixel(screen, currentTail.x,  currentTail.y))) {
  88.    Ellipse(screen, currentTail.x - size, currentTail.y - size,
  89.                    currentTail.x + size, currentTail.y + size);
  90.  
  91.    }
  92.    /* display new head */
  93.    currentHead = rP->center;
  94.    if(IsOnDesktop(currentHead)) {
  95.       Ellipse(screen, currentHead.x - size, currentHead.y - size,
  96.                       currentHead.x + size, currentHead.y + size);
  97.       rP->color = GetPixel(screen, currentHead.x,  currentHead.y);
  98.       rP->state = ACTIVE;
  99.    }
  100.    else
  101.       rP->state = INACTIVE;
  102.  
  103.    /* release the device context as soon as possible */
  104.    DeleteDC(screen);
  105. }
  106.  
  107. /* -------------------------------------------------------------- */
  108.  
  109. void CleanUp(void)
  110. {
  111.   HDC screen;
  112.   int i;
  113.   int size = GetSize();
  114.   POINT point;
  115.   RECT  rect;
  116.  
  117.  
  118.   screen=CreateDC("DISPLAY", NULL, NULL, NULL);
  119.  
  120.   SelectObject(screen, GetStockObject(BLACK_BRUSH) );
  121.   SetROP2(screen, R2_NOTXORPEN);
  122.   SelectObject(screen, GetStockObject(BLACK_PEN) );
  123.  
  124.   for(i=0; i < GetRings() ; i++) {
  125.    point = rP->center;
  126.    if((IsOnDesktop(point))  && (rP->state == ACTIVE) &&
  127.       (rP->color == GetPixel(screen, point.x,  point.y)))
  128.       rect.left   = point.x - size;
  129.       rect.top    = point.y - size;
  130.       rect.right  = point.x + size;
  131.       rect.bottom = point.y + size;
  132.       InvalidateRect(GetDesktopWindow(), &rect, TRUE);
  133.       //Ellipse(screen, point.x - size, point.y - size,
  134.        //               point.x + size, point.y + size);
  135.    rP->state = INACTIVE;
  136.    rP = rP->next;
  137.   } /* for */
  138.  
  139.   DeleteDC(screen);
  140. }
  141.  
  142. /* ------------------------------------------------------------------- */
  143.  
  144.  
  145. /* EOF */